package com.wxmit.framework.excel.handler;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.EnumUtil;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.wxmit.framework.excel.annotation.ExcelCellSelect;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

/**
 *
 * easyExcel 导出(空)模版的处理类
 * @author huangxiong
 * @version V1.0
 * @date 2023/1/5 15:46
 */
public class ExcelExportTemplateHandler implements CellWriteHandler {

    // 格式终止行
    final Integer endRowIndex = 501;

    @Override
    public void beforeCellCreate(CellWriteHandlerContext context) {
        if(!context.getHead()){
            CellWriteHandler.super.beforeCellCreate(context);
            return;
        }

        WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
        Sheet sheet = writeSheetHolder.getSheet();

        handleExcelCellSelect(context);

        CellWriteHandler.super.beforeCellCreate(context);
    }
    @SneakyThrows
    private void handleExcelCellSelect(CellWriteHandlerContext context) {
        // 字段
        ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
        Field field = excelContentProperty.getField();

        ExcelCellSelect annotation = field.getAnnotation(ExcelCellSelect.class);
        if(Objects.isNull(annotation)){
            return;
        }
        String[] value = annotation.value();

        if(value.length == 0){
            ExcelCellSelectFieldHandler excelCellSelectFieldHandler = annotation.handler().newInstance();
            if(!(excelCellSelectFieldHandler instanceof DefExcelCellSelectFieldHandler)){
               value =  ArrayUtil.toArray(excelCellSelectFieldHandler.getCellSelectDate(field.getName()),String.class);
            }else{
                Class<? extends Enum<?>> enumClass = annotation.enumClass();
                List<Object> fieldValues = EnumUtil.getFieldValues(enumClass, annotation.enumField());
                value = fieldValues.stream().map(it->it==null?"":it.toString()).toArray(String[]::new);
            }
        }
        // 起始行
        Integer firstRow = context.getRowIndex() + 1;

        // 列
        Integer columnIndex = context.getColumnIndex();

        Sheet sheet = context.getWriteSheetHolder().getSheet();
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 加载下拉列表内容
        DataValidationConstraint constraint = helper.createExplicitListConstraint(value);
        // 设置数据有效性加载在哪个单元格上,四个参数分别是：起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRowIndex, columnIndex, columnIndex);
        // 数据有效性对象
        DataValidation dataValidation = helper.createValidation(constraint, regions);
        // 处理Excel兼容性问题
        if (dataValidation instanceof XSSFDataValidation){
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        }
        else{
            dataValidation.setSuppressDropDownArrow(false);
        }

        sheet.addValidationData(dataValidation);
    }
}
